package com.sumer.psychology.comment.resource;

import com.sumer.psychology.comment.application.command.ChangeCommentCommand;
import com.sumer.psychology.comment.application.command.CreateCommentCommand;
import com.sumer.psychology.comment.application.command.DeleteCommentCommand;
import com.sumer.psychology.comment.domain.model.CommentId;
import com.sumer.psychology.comment.domain.model.CommentType;
import com.sumer.psychology.common.application.ApplicationServiceRegistry;
import com.sumer.psychology.common.resource.AbstractResource;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

/**
 * 评论rest接口
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/9/24
 * Time: 15:12
 */
@Path("/v1/comment/")
@Consumes(MediaType.APPLICATION_JSON)
public class CommentResource extends AbstractResource {
    @Context
    private UriInfo uriInfo;

    /**
     * 增加一个comment
     *
     * @param aCreateCommentCommand command
     * @return response
     */
    @POST
    public Response addComment(CreateCommentCommand aCreateCommentCommand) {
        checkAuthorAndRole(aCreateCommentCommand);

        CommentId commentId = ApplicationServiceRegistry
                .commentApplicationService()
                .newComment(aCreateCommentCommand);

        return Response
                .status(Response.Status.CREATED)
                .location(uriInfo.getAbsolutePath())
                .entity(savedId(commentId.identity()))
                .build();
    }

    @PUT
    @Path("{commentId}")
    @Deprecated
    public Response changeComment(
            @PathParam("commentId") String commentId,
            ChangeCommentCommand aChangeCommentCommand) {
        ApplicationServiceRegistry
                .commentApplicationService()
                .changeContent(new ChangeCommentCommand(
                        commentId,
                        aChangeCommentCommand.changedContent()));

        return Response.status(Response.Status.ACCEPTED).build();
    }

    /**
     * 删除评论
     *
     * @param commentId comment id
     * @return response
     */
    @DELETE
    @Path("{commentId}")
    public Response deleteComment(@PathParam("commentId") String commentId) {
        ApplicationServiceRegistry
                .commentApplicationService()
                .delete(new DeleteCommentCommand(commentId));

        return Response.status(Response.Status.ACCEPTED).build();
    }

    /**
     * 查询一个宿主下的评论树
     *
     * @param hostId host id
     * @param type   type
     * @return response
     */
    @GET
    @Path("host/{hostId}")
    public Response findCommentsOfHost(
            @PathParam("hostId") String hostId,
            @QueryParam("type") String type,
            @QueryParam("offset") int offset,
            @QueryParam("countPerPage") int countPerPage) {
        String results = ApplicationServiceRegistry
                .commentQueryApplicationService()
                .findByHost(hostId, CommentType.get(type), countPerPage, offset);

        return Response.ok(results).build();
    }

}
